package com.iseastar.dianxiaosan.home.adapter;

import android.app.Activity;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.iseastar.dianxiaosan.model.ParcelAbnormalBean;
import com.iseastar.guojiang.app.Intents;
import com.shouhuobao.station.R;
import com.shouhuobao.take.utils.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import droid.frame.activity.base.BaseAdapterWithImage;

/**
 * 异常申报
 *
 * @author coffee<br>
 *         2016-12-10上午12:10:34
 */
public class ParcelAbnormalAdapter extends BaseAdapterWithImage<ParcelAbnormalBean> {

    private SimpleDateFormat sdf;

    public ParcelAbnormalAdapter(ArrayList<ParcelAbnormalBean> items, Activity context, ViewGroup viewGroup) {
        super(items, context, viewGroup);
        sdf = new SimpleDateFormat("yyyy-MM-dd");
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.parcel_abnormal_item, parent, false);
            holder.code = (TextView) convertView.findViewById(R.id.parcel_wait_code);
            holder.number = (TextView) convertView.findViewById(R.id.parcel_wait_sender_phone);
            holder.name = (TextView) convertView.findViewById(R.id.parcel_wait_sender_name);
            holder.time = (TextView) convertView.findViewById(R.id.parcel_wait_arrival_time);
            holder.photo = (ImageView) convertView.findViewById(R.id.parcel_wait_photo);
            holder.telePhone = (ImageView) convertView.findViewById(R.id.parcel_wait_phone);
            // 异常包裹信息
            holder.parcelCause = (TextView) convertView.findViewById(R.id.parcel_abnormal_cause);
            holder.parcelNumber = (TextView) convertView.findViewById(R.id.parcel_abnormal_phone);
            holder.parcelName = (TextView) convertView.findViewById(R.id.parcel_abnormal_name);
            holder.parcelTime = (TextView) convertView.findViewById(R.id.parcel_abnormal_time);
            holder.parcelPhone = (ImageView) convertView.findViewById(R.id.parcel_abnormal_phone_img);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final ParcelAbnormalBean item = items.get(position);
        //
        showImage(item.getParcelImgUrl(), holder.photo, null);

        holder.number.setText(Html.fromHtml("<font color=#888888>电话: </font>" + item.getUserPhone()));// 收件人电话
        if (TextUtils.isEmpty(item.getBagCode()) || "null".equals(item.getBagCode())) {
            holder.code.setText(Html.fromHtml("<font color=#888888>投递编号: </font>" + context.getString(R.string.parcel_nobag_code)));// 投递袋编码
        } else {
            holder.code.setText(Html.fromHtml("<font color=#888888>投递编号: </font>" + item.getBagCode()));// 投递袋编码
        }
        holder.name.setText(Html.fromHtml("<font color=#888888>下单人: </font>" + item.getUserName()));

        Date date = new Date(item.getPostTime());
        String time = sdf.format(date);
        holder.time.setText(Html.fromHtml("<font color=#888888>到店时间: </font>" + time));// 到店时间
        // 异常包裹信息
        holder.parcelNumber.setText(Html.fromHtml("<font color=#888888>电话: </font>" + item.getCourierPhone()));// 收件人电话
        if (StringUtils.isEmpty(item.getAbnormalCause())){
            holder.parcelCause.setText(Html.fromHtml("<font color=#888888>异常原因: </font>"));// 异常原因
        }else {
            holder.parcelCause.setText(Html.fromHtml("<font color=#888888>异常原因: </font>" + item.getAbnormalCause()));// 异常原因
        }
        holder.parcelName.setText(Html.fromHtml("<font color=#888888>裹将: </font>" + item.getCourierName()));

        Date date2 = new Date(item.getAbnormalTime());
        String parcelTime = sdf.format(date2);
        holder.parcelTime.setText(Html.fromHtml("<font color=#888888>申报异常时间: </font>" + parcelTime));// 到店时间

        // 寄件人电话
        holder.telePhone.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intents.dialOrCall(item.getUserPhone());
            }
        });
        // 裹将电话
        holder.parcelPhone.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intents.dialOrCall(item.getCourierPhone());
            }
        });
        return convertView;
    }

    private static class ViewHolder {
        TextView code, number, name, time, parcelCause, parcelNumber, parcelName, parcelTime;
        ImageView photo, telePhone, parcelPhone;// 包裹中寄件照片,寄件人电话
    }
}
